package game.leon.seawar.server.handler;

import game.leon.engine.net.GameSession;
import game.leon.engine.pattern.Creator;
import game.leon.engine.pattern.Fun;
import game.leon.seawar.vo.BaseVO;
import game.leon.seawar.vo.ByteBuffer;
import game.leon.seawar.vo.RemoteMessage;

import java.io.IOException;

/**
 * User: BianP
 * Date: 11-12-29
 * 一个产生消息, 消费消息的套装
 */
class HandlerSuit <T extends BaseVO> implements
        FunList<T, BaseVO>,
        Handler<ByteBuffer> {
    private FunSuit<T> funSuit;
    private Creator<T> creator;
    public HandlerSuit(Creator<T> creator, FunSuit<T> funSuit) {
        this.creator = creator;
        this.funSuit = funSuit;
    }
    @Override
    public RemoteMessage handle(GameSession session, int message, ByteBuffer buffer) throws IOException {
        T vo = creator.create();
        RemoteMessage inputMessage = new RemoteMessage(vo);
        inputMessage.unpack(buffer);
        RemoteMessage result = funSuit.handle(session, message, vo);
        if (result == null) return null;
        return result;
    }
    @Override
    public void addFun(Fun<FunParam<T>, BaseVO> fun) {
        funSuit.addFun(fun);
    }
}
